package com.sdry.web.controller.gemini;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sdry.base.BaseController;
import com.sdry.entity.*;
import com.sdry.entity.permission.UpmsStaffRole;
import com.sdry.service.gemini.UpmsDepartmentService;
import com.sdry.utils.ResponseUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.sdry.entity.UmpsDepartmentExample.Criteria;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by Gemini on 2018/7/31
 *
 * 部门信息控制类
 */

@Controller
@RequestMapping("/base/department")
public class BaseDepartmentInfoController extends BaseController {
    @Resource
    private UpmsDepartmentService departmentService;

    /**
     * 去部门信息页面
     * @return
     */
    @RequestMapping(value = "/baseInfoDepartment", method = RequestMethod.GET)
    public String baseInfoDepartment() {

        return "/gemini/info_department";
    }

    /**
     * 添加部门信息
     * @param response 响应
     * @param department 部门实体
     * @throws Exception 异常
     */
    @RequestMapping("/insertBaseDepartmentInfo")
    public void insertDepartment(HttpServletRequest request,HttpServletResponse response, UmpsDepartment department) throws Exception {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        department.setDeptTime(df.format(new Date()));//给录入时间赋值
        //判断session
        HttpSession session  = request.getSession();
        //从session中取出用户身份信息
        UpmsStaffRole staffSession = (UpmsStaffRole) session.getAttribute("user");

        String name = null;
        if(staffSession != null){
            name = staffSession.getUpmsStaff().getSname();
        }
        department.setDeptEntry(name);
        //添加
        int affact = departmentService.insert(department);
        ResponseUtil.write(response, affact);
    }

    /**
     * 带条件的分页查询部门信息
     * @param request 请求
     * @param response 响应
     * @param limit 每页记录数
     * @param offset 页面
     * @param department 部门实体
     * @throws Exception 异常
     */
    @RequestMapping("/selectBaseDepartmentInfo")
    public void selectDepartmentList(HttpServletRequest request, HttpServletResponse response,
                               @RequestParam(value = "limit",required = false)int limit,
                               @RequestParam(value = "offset",required = false)int offset,
                                     UmpsDepartment department) throws Exception {

        PageHelper.startPage(offset, limit);

        UmpsDepartmentExample example = new UmpsDepartmentExample();
        Criteria criteria = example.createCriteria();
        if (department.getDeptName() != null && !("").equals(department.getDeptName())){
            criteria.andDeptNameLike("%"+department.getDeptName()+"%");
        }

        List<UmpsDepartment> departments = departmentService.selectByExample(example);

        PageInfo pageInfo = new PageInfo(departments);

        JSONObject jsonObject=new JSONObject();
        jsonObject.put("total", pageInfo.getTotal());
        jsonObject.put("rows", pageInfo.getList());
        ResponseUtil.write(response, jsonObject);
    }


    /**
     * 查询所有部门信息
     * @param request 请求
     * @param response 响应
     * @param department 部门实体
     * @throws Exception 异常
     */
    @RequestMapping("/selectAllDepartment")
    public void selectAllDepartment(HttpServletRequest request, HttpServletResponse response,
                                    UmpsDepartment department) throws Exception {

        UmpsDepartmentExample example = new UmpsDepartmentExample();
        List<UmpsDepartment> departments = departmentService.selectByExample(example);
        JSONArray array=JSONArray.fromObject(departments);
        ResponseUtil.write(response, array);
    }



    /**
     * 根据id查询部门信息
     * @param response 响应
     * @param id 部门id
     * @throws Exception 异常
     */
    @RequestMapping("/selectBaseDepartmentInfoById")
    public void selectDepartmentById(HttpServletResponse response, Long id) throws Exception {

        UmpsDepartment department = departmentService.selectByPrimaryKey(id);
        //将对象转化为json
        JSONObject json = JSONObject.fromObject(department);
        ResponseUtil.write(response, json);
    }

    /**
     * 修改部门信息
     * @param response 响应
     * @param department 部门信息
     * @throws Exception 异常
     */
    @RequestMapping("/editBaseDepartmentInfo")
    public void editDepartment( HttpServletRequest request,HttpServletResponse response,
                                UmpsDepartment department) throws Exception {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        department.setDeptTime(df.format(new Date()));//给录入时间赋值
        //判断session
        HttpSession session  = request.getSession();
        //从session中取出用户身份信息
        UpmsStaffRole staffSession = (UpmsStaffRole) session.getAttribute("user");

        String name = null;
        if(staffSession != null){
            name = staffSession.getUpmsStaff().getSname();
        }
        department.setDeptEntry(name);
        //修改
        int affct = departmentService.updateByPrimaryKey(department);
        ResponseUtil.write(response, affct);
    }

    /**
     * 批量删除部门信息
     * @param response 响应
     * @param ids 部门id
     * @throws Exception 异常
     */
    @RequestMapping("/deleteBaseDepartmentInfo")
    public void deleteDepartment( HttpServletResponse response, String ids) throws Exception {
        int affact = departmentService.deleteByPrimaryKeys(ids);
        ResponseUtil.write(response, affact);
    }
}
